package com.qdaily.ui.ui.views;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.qdaily.supportlibrary.util.ImageUtil;
import com.qdaily.supportlibrary.util.LocalDisplay;
import com.qdaily.supportlibrary.views.banner.BannerAdapter;
import com.qdaily.supportlibrary.views.banner.DotView;
import com.qdaily.supportlibrary.views.banner.SliderBanner;
import com.qdaily.ui.R;
import com.qdaily.ui.controller.ActivityController;
import com.qdaily.ui.net.model.ArticleFeeds;
import com.qdaily.ui.net.model.ArticleMeta;

/**
 * 主页广告轮播图
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/7/15
 * Time: 16:13
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class SliderBannerController {

    public final static int Height = (int) (LocalDisplay.SCREEN_WIDTH_PIXELS * 1f / 16 * 9.6f);
    private SliderBanner mSliderBanner;
    private InnerAdapter mBannerAdapter = new InnerAdapter();
    private DotView mDotView;
    private final int TIMEINTERVAL = 5 * 1000;
    private Context mContext;
    private float mStartDragX;

    public SliderBannerController(Context context, SliderBanner sliderBanner) {
        this.mContext = context;
        mDotView = (DotView) sliderBanner.findViewById(R.id.dotViewBannerIndicator);

        mSliderBanner = sliderBanner;

        sliderBanner.setAdapter(mBannerAdapter);

        //父类的事件拦截。banner的viewpager滑动与父类的viewpager互斥。
        mSliderBanner.setViewPagerOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_DOWN && v instanceof ViewGroup) {
                            ((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
                        }

                        if (mSliderBanner.getCurrentItem() == mBannerAdapter.getCount() - 1) {
                            final int action = event.getAction();
                            float x = event.getX();
                            switch (action & MotionEventCompat.ACTION_MASK) {
                                case MotionEvent.ACTION_DOWN:
                                    mStartDragX = x;
                                    break;
                                case MotionEvent.ACTION_MOVE:
                                    break;
                                case MotionEvent.ACTION_UP:
                                    if (x < mStartDragX) {
                                        rePlay();
                                    } else {
                                        mStartDragX = 0;
                                    }
                                    break;
                            }
                        } else {
                            mStartDragX = 0;
                        }
                        return true;
                    }
                }
        );
    }

    private void rePlay() {
    }

    public void play(ArticleFeeds list) {
        if (list.getList().size() == 0) {
            return;
        }
        mBannerAdapter.cleaData();
        mBannerAdapter.setData(list);
        mBannerAdapter.notifyDataSetChanged();
        mSliderBanner.setDotNum(list.getList().size());
        if (!mSliderBanner.isPlaying()) {
            mSliderBanner.setTimeInterval(TIMEINTERVAL);
        }
        mSliderBanner.stopPlay();
        mSliderBanner.beginPlay();
    }

    private class InnerAdapter extends BannerAdapter {
        private ArticleFeeds mDataList;

        public void setData(ArticleFeeds data) {
            mDataList = data;
        }

        public void cleaData() {
            if (mDataList != null) {
                mDataList = null;
            }
        }

        public ArticleMeta getItem(int position) {
            if (mDataList == null)
                return null;
            return mDataList.getList().get(position);
        }

        @Override
        public int getPositionForIndicator(int position) {
            if (null == mDataList || mDataList.getList().size() == 0) {
                return 0;
            }
            return position % mDataList.getList().size();
        }

        @Override
        public View getView(LayoutInflater layoutInflater, int position) {
            View convertView = layoutInflater.inflate(R.layout.view_article_match, null);

            final ArticleMeta item = getItem(position);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.cubeImageViewArticleMatch);
            ImageUtil.getInstance().displayImage(item.getImage(), imageView);

            TextView textViewBannerCategory = (TextView) convertView.findViewById(R.id.textViewArticleCategory);
            String result = "";
            if (!TextUtils.isEmpty(item.getPost().getSuper_tag())) {
                result = item.getPost().getCategory().getTitle() + " · " + item.getPost().getSuper_tag();
            }
            else
            {
                result = item.getPost().getCategory().getTitle();
            }
            textViewBannerCategory.setText(result);

            TextView textViewBannerTitle = (TextView) convertView.findViewById(R.id.textViewArticleMatchTitle);
            textViewBannerTitle.setText(item.getPost().getTitle());

            ImageView cubeImageViewBannerCategory = (ImageView) convertView.findViewById(R.id.cubeImageViewArticleCategory);
            ImageUtil.getInstance().displayImage(item.getPost().getCategory().getImage_small(),
                    cubeImageViewBannerCategory);

            convertView.setTag(item);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    displayArticleDetails(item);
                }
            });

            return convertView;
        }

        private void displayArticleDetails(ArticleMeta articleMeta) {
            ActivityController.DisplayDetailActivity(mContext, articleMeta);
        }

        @Override
        public int getCount() {
            if (mDataList == null) {
                return 0;
            }
            return mDataList.getList().size();
        }
    }

}
